<?php
namespace app\admin\controller;

use think\Loader;
use think\Db;

class Activity extends Common
{

    private $obj;

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        /**
         * 获取活动列表1
         */
        $data = db('article')->where('category_id', 24)->order('id desc')->paginate();
        $this->assign('data', $data);
        return view();
    }

    public function show_num() {
        /**
         * 获取活动列表
         */
        $id = input('param.id');
        $this->assign('id', $id);
            $data = db('huodongbaoming')->where('activity_id', $id)->order('id desc')->select();
        $this->assign('data', $data);
        return view();
    }
    /*获取兄弟连信息*/
    public function showlist(){
        $id = input('param.id');
        $this->assign('id', $id);
        $data = db('brothers')->where('activity_id', $id)->order('id desc')->select();
        $this->assign('data', $data);
        return view();
    }
    public function show_fbh(){
        $id = input('param.id');
        $this->assign('id', $id);
        $data = db('fbh')->order('id desc')->select();
        $this->assign('data', $data);
        return view();
    }
    public function show_fbh1(){
    $id = input('param.id');
    $this->assign('id', $id);
    $data = db('fbh1')->order('id desc')->select();
    $this->assign('data', $data);
    return view();
}
    public function fbher(){
        $id = input('param.id');
        $this->assign('id', $id);
        $data = db('fbher')->order('id desc')->select();
        $this->assign('data', $data);
        return view();
    }
    /*下载word文档*/

    public function download_word(){
        $id = input('param.id');
        $list = db('brothers')->alias('b')
            ->join('position p','b.duty = p.id')
            ->join('region r','b.united =r.id')
            ->where('b.id', $id)->find();
        $image = \think\Image::open($list['head']);
// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.pngs
        $img = 'uploads/thumb'.time().'png';
        $image->thumb(151, 166)->save($img);
        $html='<!DOCTYPE HTML>
<html>
<head><style>.main{ width: 666px;margin: 0 auto;}table{border-collapse :collapse ;}thead tr td:nth-of-type(1),thead tr td:nth-of-type(3){width:100px;}th,td{
            width:150px; height:30px;border :1px solid black;font-size:14px;text-align :center;}tfoot td.left{text-align :left; padding: 10px;}.bg-e0{background: #E0E0E0;}.work{height: 150px;}.skill{height: 40px;}
        .like{height: 50px;}.world{height: 80px;}.evaluate{height: 100px;}</style></head><body>
<div class="main">
    <table>
        <thead>
             <tr>
                <td class="bg-e0">姓 名</td>
                <td>'.$list["name"].'</td>
                <td class="bg-e0">性 别</td>
                <td>'.$list["sex"].'</td>
                <td rowspan="5" class="imgs"><img height="151" width="auto"  src="http://www.hnjyxh.com/'.$img.'"/></td>
            </tr>
            <tr>
                <td class="bg-e0">民  族</td>
                <td>'.$list["national"].'</td>
                <td class="bg-e0">出生年月</td>
                <td>'.date("Y-m-d",$list["birthday"]).'</td>
            </tr>
            <tr>
                <td class="bg-e0">身  高</td>
                <td>'.$list["height"].'cm</td>
                <td class="bg-e0">现居住地</td>
                <td>'.$list["live"].'</td>
            </tr>
            <tr>
                <td class="bg-e0">学  历</td>
                <td>'.$list["education"].'</td>
                <td class="bg-e0">专  业</td>
                <td>'.$list["majors"].'</td>
            </tr>
            <tr>
                <td class="bg-e0">政治面貌</td>
                <td>'.$list["politik"].'</td>
                <td class="bg-e0">兄弟连职务</td>
                <td>'.$list["position"].'</td>
            </tr>
            <tr>
                <td class="bg-e0">籍  贯</td>
                <td>'.$list["native"].'</td>
                <td class="bg-e0">毕业学校</td>
                <td colspan="2">'.$list["school"].'</td>
            </tr>
            <tr>
                <td class="bg-e0">推荐人</td>
                <td>'.$list["recommend"].'</td>
                <td class="bg-e0">推荐人电话</td>
                <td colspan="2">'.$list["recommendtel"].'</td>
            </tr>
            <tr>
                <td class="bg-e0">QQ 号码</td>
                <td>'.$list["QQ"].'</td>
                <td class="bg-e0">所属大区联队</td>
                <td colspan="2">'.$list["region"].'</td>
            </tr>
            <tr>
                <td class="bg-e0">紧急联系人</td>
                <td>'.$list["contact"].'</td>
                <td class="bg-e0">联系人电话</td>
                <td colspan="2">'.$list["contactel"].'</td>
            </tr>
            <tr>
                <td class="bg-e0 work">省市区</td>
                <td colspan="4" class="left">'.$list["dizi"].'</td>
            </tr>
        </thead>
        <tbody>

            <tr>
                <td class="bg-e0">联系方式</td>
                <td>电话</td>
                <td>'.$list["tel"].'</td>
                <td>邮箱</td>
                <td>'.$list["email"].'</td>
            </tr>
        </tbody>
        <tfoot>

            <tr>
                <td class="bg-e0 work">工作经历</td>
                <td colspan="4" class="left">'.$list["experience"].'</td>
            </tr>

            <tr>
                <td class="bg-e0 like">兴趣爱好</td>
                <td colspan="4" class="left">'.$list["hobby"].'</td>
            </tr>
            <tr>
                <td class="bg-e0 world">个人荣誉</td>
                <td colspan="4" class="left">'.$list["practice"].'</td>
            </tr>
            <tr>
                <td class="bg-e0 evaluate">个性评价</td>
                <td colspan="4" class="left">'.$list["evaluation"].'</td>
            </tr>
        </tfoot></table></div></body></html>';
        $this->start();
        $wordname='兄弟连个人简介word.doc';
        echo $html;
        $this->save($wordname);
        header('Content-type:application/word');
        header('Content-Disposition: attachment; filename='.$wordname.'');
//readfile($wordname);
        ob_flush();//每次执行前刷新缓存
        flush();


    }
    function start(){
        ob_start();
        echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns="http://www.w3.org/TR/REC-html40">';
    }
    function save($path)
    {
        echo "</html>";
        $data = ob_get_contents();
    }
    function wirtefile($fn,$data){
        $fp=fopen($fp,$data);
        fwrite($fp,$data);
    }

    public function download_excel() {
        $id = input('param.id');
        $data = db('huodongbaoming')->field('id, enterprise, linkman, duty, mobile')->where('activity_id', $id)->order('id desc')->select();
        //dump($data);
        excelExport('participator', ['ID', '单位', '联系人', '职务', '电话'], $data);
    }
    public function download_sexcel()
    {
        $id = input('param.id');
        $data = db('brothers')->alias('b')->join('region r', 'b.united=r.id', 'left')->field('b.id, b.name,b.native, b.duty, b.tel, b.live , b.QQ ,b.email,r.region ,b.contact,b.contactel')->where('b.activity_id', $id)->order('b.id desc')->select();


        excelExport('participator', ['ID', '姓名', '省联队', '职务', '电话', '住址', 'QQ', '电子邮箱', '所属大区联队', '紧急联系人', '紧急联系人电话'], $data);
    }
    public function download_excel2() {
        $id = input('param.id');
        $data = db('fbh')->field('id, name, sex, unit, tel')->order('id desc')->select();
        //dump($data);
        excelExport('participator', ['ID', '姓名', '性别', '单位', '电话'], $data);
    }
    public function download_excel3() {
        $id = input('param.id');
        $data = db('fbh1')->field('id, name, sex, unit, tel')->order('id desc')->select();
        //dump($data);
        excelExport('participator', ['ID', '姓名', '性别', '单位', '电话'], $data);
    }
    public function download_excel4() {
        $id = input('param.id');
        $data = db('fbher')->field('id, name, sex, unit, tel')->order('id desc')->select();
        //dump($data);
        excelExport('participator', ['ID', '姓名', '性别', '单位', '电话'], $data);
    }
    public function download_excel5() {
        $id = input('param.id');
        $data = db('advert')->field('id, username, mobile, advert')->order('id desc')->select();
        //dump($data);
        excelExport('participator', ['ID', '姓名', '手机号', '广告语'], $data);
    }
    public function download_excel6() {
        $data = db('advert')->field('id , advert')->where(array('status'=>1,'delete'=>0))->select();
        excelExport('participator', ['ID', '广告语'], $data);
    }
    public function download_excel7() {
        $data = db('drink')->field('id , username, sex, tel, company, job')->select();
        foreach($data as $k => $v){
            if($v['sex'] == 1){
                $data[$k]['sex'] = '男';
            }else{
                $data[$k]['sex'] = '女';
            }
        }
        excelExport('participator', ['ID', '姓名', '性别', '手机号码', '单位', '职务'], $data);
    }
    public function download_excel8() {
        $data = db('advert_vote')->field('id , advert, count')->select();
        excelExport('participator', ['ID', '广告语', '票数'], $data);
    }
    public function zjjz() {
        /**
         * 获取征集酒字活动列表
         */
        $data = db('zjjz')->order('id desc')->paginate();
        $this->assign('data', $data);
        return view();
    }
    /**
     * 获取征集珍湘酒广告语活动列表
     */
    public function advert() {
        $data = db('advert')->where('delete',0)->order('id desc')->paginate();
        $this->assign('data', $data);
        return view();
    }
    public function advertexcel(){
        $db = Db::connect('mysql://jxtx_db:jxtx_db@2017@101.132.32.60:3306/jxtx#utf8');

        $data = $db->table('jx_xydzp_address')->field('id,link_man,mobile,address,xydzp_id')->select();

        foreach($data as $k => $v){
            $data[$k]['xydzp_id'] = '三等奖';
        }

        excelExport('participator', ['ID','姓名','联系电话','地址','奖品等级'], $data);
    }

    /**
     * 审核珍湘酒广告语
     * */
    public function save_advert(){
        $id = input('param.id', 0);
        if (request()->isPost()) {
            /**
             * 组装数据
             */
            $data['status'] = input('post.status', '');
            if ($create_time = input('post.create_time')) {
                $data['create_time'] = strtotime($create_time);
            }

            /**
             * 保存数据
             */
            $where = $id > 0 ? ['id' => $id] : [];
            $rs = model('advert')->save($data, $where);

            // 清理缓存
            \app\api\service\Base::clear_cache();

            if ($rs) {
                echo '<script>alert("保存成功"); window.parent.location.href="'. '/admin/activity/advert' .'"</script>';
                //$this->success('保存成功', '');
            } else {
                $this->error('保存失败');
            }
        } else{
            $data = [];
            if ($id > 0) {
                $data = model('advert')->get(['id' => $id]);
            }
            $this->assign('data', $data);
            return view();
        }
    }
    public function auditing(){
        $id = $_GET['id'];
        $status = $_GET['type'];//1:审核通过；-1审核失败
        $where = [
          'id' => $id
        ];
        $data = [
            'status' => $status
        ];
        $rs = model('advert')->save($data, $where);

        // 清理缓存
        \app\api\service\Base::clear_cache();

        if ($rs) {
            echo '<script>alert("操作成功"); window.parent.location.href="'. '/admin/activity/advert' .'"</script>';
            //$this->success('保存成功', '');
        } else {
            $this->error('操作失败');
        }
    }
    /**
     * 中国千商大会博鳌酒业峰会报名列表
     * */
    public function enlist(){
        $data = model('enlist')->order('id desc')->paginate();
        $this->assign('data', $data);
        return view();
    }

    public function get_qr_code() {
        $order_id = input('param.order_id');
        // 二维码不存在 生成二维码
        Loader::import('QRcode.phpqrcode');

        // 处理路径
        $root_path = ROOT_PATH . 'public' . DS;
        $upload_path = 'uploads' . DS . 'qr_code' . DS;
        $file_name = $order_id . '.png';
        $dir = $root_path . $upload_path;
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }

        $erweima_image = $upload_path . $file_name; // 出库二维码地址
        $erweima_url = $root_path . $erweima_image;

        $erweima_text = input('param.url');

        \QRcode::png($erweima_text, $erweima_url, '', 6, 2); // 生成二维码

        $logo = $root_path . 'static/home/images/qr_logo.png';

        erweima_add_logo($erweima_url, $logo); // 添加logo

        return json(['code' => 200, 'path' => $erweima_image]);
    }
    /**
     * 理性饮酒周启动仪式邀请函
     * */
    public function drink(){
        $data = model('drink')->order('id desc')->paginate();
        $this->assign('data', $data);
        return view();
    }
    /**
     * 珍湘酒广告语投票详情
     * */
    public function vote(){
        $data = model('advert_vote')->select();
        $this->assign('data',$data);
        return view();
    }
    /**
     * 珍湘酒作品征集列表
     * */
    public function works(){
        $data = model('works')->paginate(7);
        foreach($data as $k => $v){
            $data[$k]['create_time'] = date('Y-m-d H:i',$v['create_time']);
        }
        $this->assign('data',$data);
        return view();
    }
}
